package datastructure.stack;

import java.util.Stack;

public class NextGreaterElement {
    public int[] nextGreaterElement(int[] nums) {
        int n = nums.length;
        int[] res = new int[nums.length];
        int i = n - 1;
        Stack<Integer> stack = new Stack<>();
        while (i >= 0) {
            while (!stack.isEmpty() && stack.peek() <= nums[i]) {
                stack.pop();
            }
            res[i] = stack.isEmpty() ? -1 : stack.peek();
            stack.push(nums[i]);
            i--;
        }
        return res;
    }

}
